const getDefaultState = () => {
  return {
    allInfo: new FormData(),// 最终存放spu和sku（文字+图片）
    spu: {
      productId: '',// 产品id
      categoryAndBrand: [],// 类别和品牌
      name: '',// 名称
      title: '',// 标题
      describe: '',// 描述
      detail: '',// 详情
      attributeList: [// 属性
        /*      { key:'内存', value: ["8GB+128GB"] },*/
        { key:'规格1', value: ["规格1"] },
      ],
      dbAttributeList: {"规格1":["规格1"]},
      price: 1.00,
      state: 1,
    },
    skuList: [],// sku信息列表
    disabled: false,// spu是否锁定
    skuDisabledList: [],// sku列表是否锁定
  }
}

//准备state，用于存储数据，菜品
const state = getDefaultState()

//准备mutations，用于操作数据 state，厨师
const mutations = {
  //state:数据  value:传进来的参数
  reset: (state) => {
    Object.assign(state, getDefaultState())
  },

  // 设置spu锁定状态
  SET_DISABLED: (state, value) => {
    //console.log("mutations SET_SPU",value)
    state.disabled = value
  },

  // 设置spu信息
  SET_SPU: (state, value) => {
    //console.log("mutations SET_SPU",value)
    state.spu = value
  },

  // 设置attributeList
  SET_ATTRIBUTE_LIST: (state, value) => {//
    state.spu.attributeList = value
  },

  // 设置dbAttributeList
  SET_DB_ATTRIBUTE_LIST: (state, value) => {//
    //console.log("SET_DB_ATTRIBUTE_LIST", value)
    state.spu.dbAttributeList = value
  },

  // 设置allInfo信息，追加写入
  SET_ALL_INFO: (state, value) => {
    //state.allInfo = value;
    console.log("SET_ALL_INFO = ",value)
    state.allInfo.append(value.key, value.value);
  },

  // 清空allInfo信息
  CLEAR_ALL_INFO: (state, value) => {
    state.allInfo = new FormData();
  },

  // 清空allInfo中的skuList信息
  clear_allInfo_skuList: (state, value) => {
    state.allInfo.delete("skuJson");
  },

  /* sku */
  // 添加sku数据
  SET_SKU_LIST: (state, value) => {
    state.skuList.push(value)
  },

  // 移除sku数据
  REMOVE_SKU_LIST: (state, index) => {
    state.skuList.splice(index, 1)
  },

  // 清空sku数据，在解除spu时触发
  init_sku_list: (state, value) => {
    state.skuList = []
  },

  // 初始化商品sku锁定列表，默认都不锁定
  INIT_SKU_DISABLED_LIST: (state, count) => {
    state.skuDisabledList = new Array(count).fill(false);
  },

  // 根据 下标和数据 更新商品sku锁定列表
  UPDATE_SKU_DISABLED_LIST: (state, value) => {
   /* {index: this.index,value: true }*/
    console.log(value)
    state.skuDisabledList.splice(value.index,1,value.value)
    console.log(state.skuDisabledList)
  },

}

//准备actions，用于响应组件的动作，服务员
const actions = {
  addView({ dispatch }, view) {//context:上下文迷你版store  value:传进来的参数
    dispatch('addVisitedView', view)
    dispatch('addCachedView', view)
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
